import Foundation
import CreateML
import CreateMLUI


var str = "Hello, playground"

let stra:String = "/Users/mac/Documents/spam-sms.csv"
//let stra:String = "/Users/mac/Downloads/HouseData.csv"

//将csv文件内容加载到MLDataTable中

var spamData = try MLDataTable(contentsOf: URL(fileURLWithPath: stra))

let (trainingData, testData) = spamData.randomSplit(by: 0.8, seed: 0)

//创建文本分类器，进行训练
//let predictor = try MLRegressor(trainingData: trainingData, targetColumn: "MEDV")
let predictor = try MLRegressor(trainingData: trainingData, targetColumn: "message")



//message和label分别对应csv文件中的短信内容列、短信标签列
//
//let predictor = try MLTextClassifier(trainingData: trainingData, textColumn: "label", labelColumn: "message")


//2

//let trainingAccuracy = (1.0 - predictor.trainingMetrics.classificationError) * 100
//
//let validationAccuracy = (1.0 - predictor.validationMetrics.classificationError) * 100

//在测试数据集上验证
let metrics = predictor.evaluation(on: testData)
//let evaluationAccuracy = (1.0 - metrics.classificationError) * 100

//4

let metadata = MLModelMetadata(author: "Sai Kambampati", shortDescription: "A model trained to classify spam messages", version: "1.0")


try! predictor.write(to: URL(fileURLWithPath:"/Users/mac/Documents/test.mlmodel" ), metadata: metadata)


